package org.spbsu.apco.common.task.core;

import org.spbsu.apco.common.data.core.Data;
import org.spbsu.apco.common.serialization.Serializer;

import java.util.List;

/**
 * User: ortemij
 * Date: 01.01.13
 * Time: 14:52
 */
public final class Ticket implements Data {
    private static Serializer serializer = new Serializer();

    public static void setSerializer(final Serializer serializer) {
        Ticket.serializer = serializer;
    }

    private final BaseTask<?, ?> task;
    private final List<?> arguments;

    private Ticket(BaseTask<?, ?> task, List<?> arguments) {
        this.task = task;
        this.arguments = arguments;
    }

    public Ticket(byte[] bytes) {
        Ticket inner = serializer.deserializeTaskAndInput(bytes);
        task = inner.getTask();
        arguments = inner.getArguments();
    }

    public BaseTask<?, ?> getTask() {
        return task;
    }

    public List<?> getArguments() {
        return arguments;
    }

    @Override
    public byte[] serialize() throws Exception {
        return serializer.serializeTaskAndInput(task, arguments);
    }

    public static <T> Ticket of(BaseTask<T, ?> task, List<T> arguments) {
        return new Ticket(task, arguments);
    }
}
